package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.constant.CacheConstant;
import com.qs.exception.ServiceException;
import com.qs.mapper.CollectGoodsMapper;
import com.qs.mapper.CommentMapper;
import com.qs.mapper.GoodsMapper;
import com.qs.model.dto.GoodsIndexDto;
import com.qs.model.entity.CollectGoods;
import com.qs.model.entity.Comment;
import com.qs.model.entity.Goods;
import com.qs.response.ResponseEnum;
import com.qs.service.ICollectGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 收藏的商品表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-24
 */
@Service
@RequiredArgsConstructor
public class CollectGoodsServiceImpl extends ServiceImpl<CollectGoodsMapper, CollectGoods> implements ICollectGoodsService {

    private final GoodsMapper goodsMapper;
    private final CommentMapper commentMapper;

    @Override
    public void handleCollectGoods(CollectGoods goods) {
        QueryWrapper<CollectGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",goods.getUserId());
        wrapper.eq("goods_id",goods.getGoodsId());
        CollectGoods collectGoods = baseMapper.selectOne(wrapper);
        if(null != collectGoods) {
            throw new ServiceException(ResponseEnum.HAVE_COLLECTED);
        }
        baseMapper.insert(goods);
    }

    @Override
    public List<GoodsIndexDto> queryCollectGoods(Integer userId) {
        QueryWrapper<CollectGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<CollectGoods> collectGoods = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(collectGoods)) {
            List<Integer> goodsIds = collectGoods.stream().map(item -> item.getGoodsId()).collect(Collectors.toList());
            List<Goods> goodsList = goodsMapper.selectBatchIds(goodsIds);
            if(!CollectionUtils.isEmpty(goodsList)){
                List<GoodsIndexDto> resultList = goodsList.stream().map(goods -> {
                    //获取每个商品的收藏量
                    Long collectNum = baseMapper.selectCount(new QueryWrapper<CollectGoods>()
                            .eq("goods_id", goods.getId()));
                    //获取每个商品的评论量
                    Long commentNum = commentMapper.selectCount(new QueryWrapper<Comment>()
                            .eq("goods_id", goods.getId()));

                    GoodsIndexDto dto = new GoodsIndexDto();
                    dto.setId(goods.getId());
                    dto.setGoodsName(goods.getName());
                    dto.setImage(goods.getImage());
                    dto.setPrice(goods.getPrice());
                    dto.setBrowse(goods.getBrowse());
                    dto.setSale(goods.getSale());
                    dto.setCollectNum(collectNum);
                    dto.setCommentNum(commentNum);
                    return dto;
                }).collect(Collectors.toList());
                return resultList;
            }
        }
        return null;
    }
}
